/*
 * Copyright (c) 2010 Mattes Groeger
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package de.mgroeger.air.icon.example.web 
{
	import de.mattesgroeger.air.icon.IconBadge;
	import de.mattesgroeger.air.icon.IconBadgeFactory;

	import mx.controls.Image;

	public class WebIconBadgeFactory implements IconBadgeFactory 
	{
		private var image : Image;
		private var badgeType : BadgeType;

		public function WebIconBadgeFactory(image : Image, badgeType : BadgeType) 
		{
			this.image = image;
			this.badgeType = badgeType;
		}
	
		public function create() : IconBadge
		{
			return new WebIconBadge(image, badgeType);
		}
	}
}

import de.mgroeger.air.icon.example.windows.SystemTrayIconBuilder;
import de.mattesgroeger.air.icon.AbstractApplicationIconBadge;
import de.mattesgroeger.air.icon.background.BackgroundRetriever;
import de.mattesgroeger.air.icon.builder.DockIconBuilder;
import de.mattesgroeger.air.icon.builder.IconBuilder;
import de.mattesgroeger.air.icon.updater.IconUpdater;
import de.mgroeger.air.icon.example.web.BadgeType;

import mx.controls.Image;
import mx.core.BitmapAsset;

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.PixelSnapping;
import flash.events.Event;
import flash.events.EventDispatcher;

class WebIconBadge extends AbstractApplicationIconBadge
{
	private var image : Image;
	private var badgeType : BadgeType;

	public function WebIconBadge(image : Image, badgeType : BadgeType) 
	{
		this.image = image;
		this.badgeType = badgeType;
		
		super();
	}
	
	override protected function createBackgroundRetriever() : BackgroundRetriever 
	{
		return new WebBackgroundRetriever();
	}

	override protected function createIconBuilder() : IconBuilder 
	{
		switch(badgeType)
		{
			case BadgeType.WINDOWS:
				return new SystemTrayIconBuilder();
				
			default:
				return new DockIconBuilder();
		}
	}
	
	override protected function createIconUpdater() : IconUpdater 
	{
		return new WebIconUpdater(image);
	}
}

class WebBackgroundRetriever extends EventDispatcher implements BackgroundRetriever
{
	[Embed(source="../../../../../../../embed/Icon_128x128.png")]
	public var customIconClass : Class;
	
	private var _background : BitmapAsset;

	public function retrieve() : void
	{
		_background = new customIconClass() as BitmapAsset;
		
		dispatchEvent(new Event(Event.COMPLETE));
	}
	
	public function get background() : Bitmap
	{
		return _background;
	}
}

class WebIconUpdater implements IconUpdater
{
	private var image : Image;
	
	public function WebIconUpdater(image : Image) 
	{
		this.image = image;
		
		super();
	}

	public function canUpdate() : Boolean
	{
		return true;
	}
	
	public function update(icon : BitmapData) : void
	{
		image.data = new BitmapAsset(icon, PixelSnapping.AUTO, true);
	}
}